package cn.xi.controller;

import cn.xi.constant.MessageConstant;
import cn.xi.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/UsernameController")
public class UsernameController {

    @RequestMapping("/user")
    public Result user() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal == null) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        String username = principal.getUsername();

        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
    }
}
